//
//  WJPublishWordViewController.m
//  BaiSiBuDeJie
//
//  Created by jerrywang on 2017/10/15.
//  Copyright © 2017年 王军. All rights reserved.
//

#import "WJPublishWordViewController.h"
#import "WJPlaceholderTextView.h"
#import "WJPublishWordToolbar.h"

@interface WJPublishWordViewController () <UITextViewDelegate>

//文本框
@property (nonatomic, weak) WJPlaceholderTextView *textView;
//工具条
@property (nonatomic, weak) WJPublishWordToolbar *toolBar;

@end

@implementation WJPublishWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航条
    [self setupNavBar];
    
    //设置默认文字
    [self setupTextView];
    
    //设置工具条
    [self setupToolbar];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //textView变为第一响应者
    [self.textView becomeFirstResponder];
}

- (void)setupNavBar {
    self.title = @"发表文字";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:self
                                                                            action:@selector(cancel)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(post)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    // 强制更新(能马上刷新现在的状态)
    [self.navigationController.navigationBar layoutIfNeeded];
}

- (void)post {
    WJLog(@"点击发布按钮");
}

- (void)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)setupTextView{
    WJPlaceholderTextView *textView = [[WJPlaceholderTextView alloc] init];
    textView.frame = self.view.bounds;
    // 不管内容有多少，竖直方向上永远可以拖拽
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
    textView.placeholderText = @"把好玩的图片，好笑的段子或糗事发到这里，接受千万网友膜拜吧！发布违反国家法律内容的，我们将依法提交给有关部门处理。";
    [self.view addSubview:textView];
    self.textView = textView;
}

- (void)setupToolbar {
    WJPublishWordToolbar *toolbar = [WJPublishWordToolbar viewFromXib];
    toolbar.wj_x = 0;
    toolbar.wj_y = self.view.wj_height - toolbar.wj_height;
    toolbar.wj_width = self.view.wj_width;
    [self.view addSubview:toolbar];
    self.toolBar = toolbar;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

#pragma mark - 监听
- (void)keyboardWillChangeFrame:(NSNotification *)note {
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        // 工具条平移的距离 == 键盘最终的Y值 - 屏幕高度
        CGFloat ty = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y - screenHeight;
        self.toolBar.transform = CGAffineTransformMakeTranslation(0, ty);
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - <UITextViewDelegate>
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

- (void)textViewDidChange:(UITextView *)textView {
    self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
}



@end
